package com.jay.io.clazz;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedReaderWriterCls extends Thread {
	private PipedReader pr;

	private PipedWriter pw;

	PipedReaderWriterCls(String name, PipedReader pr, PipedWriter pw) {
		super(name);

		this.pr = pr;
		this.pw = pw;
	}

	public void run() {
		try {
			if (getName().equals("src")) {
				for (int i = 0; i < 15; i++)
					pw.write("src " + " A" + i + "\n");

				pw.close();
			} else {
				int item;
				while ((item = pr.read()) != -1)
					System.out.print((char) item);

				pr.close();
			}
		} catch (IOException e) {
		}
	}

	public static void main(String[] args) throws Exception {
		PipedWriter pw = new PipedWriter();
		PipedReader pr = new PipedReader(pw);

		PipedReaderWriterCls mt1 = new PipedReaderWriterCls("src", pr, pw);
		PipedReaderWriterCls mt2 = new PipedReaderWriterCls("dst", pr, pw);

		mt1.start();

		Thread.sleep(2000);
		mt2.start();
	}
}
